import { apiClient } from './axios';

// 数据字典管理相关接口
export const dictionariesApi = {
  // 获取指定类型的字典数据
  getDictionariesByType: (dictType: string) => {
    return apiClient.get(`/dictionaries/${dictType}`);
  },

  // 获取所有字典类型
  getDictionaryTypes: () => {
    return apiClient.get('/dictionaries/types');
  },

  // 获取所有字典数据（管理员用）
  getAllDictionaries: (params?: {
    page?: number;
    per_page?: number;
    dict_type?: string;
    search?: string;
  }) => {
    return apiClient.get('/dictionaries', { params });
  },

  // 创建字典项
  createDictionary: (dictionaryData: {
    dict_type: string;
    dict_key: string;
    dict_label: string;
    dict_value: string;
    sort_order?: number;
    is_active?: boolean;
    is_system?: boolean;
    parent_id?: number;
    description?: string;
  }) => {
    return apiClient.post('/dictionaries', dictionaryData);
  },

  // 更新字典项
  updateDictionary: (dictId: number, dictionaryData: {
    dict_label?: string;
    dict_value?: string;
    sort_order?: number;
    is_active?: boolean;
    parent_id?: number;
    description?: string;
  }) => {
    return apiClient.put(`/dictionaries/${dictId}`, dictionaryData);
  },

  // 删除字典项
  deleteDictionary: (dictId: number) => {
    return apiClient.delete(`/dictionaries/${dictId}`);
  },

  // 批量更新字典项
  batchUpdateDictionaries: (data: {
    ids: number[];
    action: 'activate' | 'deactivate' | 'delete';
  }) => {
    return apiClient.post('/dictionaries/batch', data);
  },

  // 复制字典项
  copyDictionary: (data: {
    source_id: number;
    new_dict_type: string;
    new_dict_key: string;
  }) => {
    return apiClient.post('/dictionaries/copy', data);
  },

  // 获取字典项的子项
  getDictionaryChildren: (dictId: number) => {
    return apiClient.get(`/dictionaries/${dictId}/children`);
  },

  // 重新排序字典项
  reorderDictionaries: (data: {
    dict_type: string;
    orders: { id: number; sort_order: number }[];
  }) => {
    return apiClient.put('/dictionaries/reorder', data);
  }
};